#!/bin/bash

who=`whoami`
tmp_loc=/tmp/rmline_$who

if [ $# -ne 2 -a $# -ne 3 ];then
	echo -e "USAGE:\trmline.sh [-r] need_deal_dir source"
	exit
fi

# $2 is the file name with the full absolute path
function rm_line() {
	local action=$1
	local file=$2
	local kind=$3
	local diff_file=$tmp_loc$file.line

	if [ "$action" == "remove" ]; then
		mv $file $file.original
		if [ "$kind" == "source" ]; then
#			more $file.original | sed -e '/^\s*\.line.*$/d' | sed -e '/^\s*\.param.*$/d' | sed -e 's/\/jumbo//' > $file
			more $file.original | sed -e '/^\s*\.line.*$/d' | sed -e 's/\/jumbo//' > $file
		else
			more $file.original | sed -e '/^\s*\.line.*$/d' | sed -e 's/\/jumbo//' > $file
		fi
		diff $file $file.original > /dev/null || {
			mkdir -p `dirname $diff_file`
			diff -B -c $file $file.original > $diff_file
		}
		rm $file.original
	else
		if [ -f $diff_file ]; then
			patch -f $file -r /dev/null < $diff_file >/dev/null 2>&1
			rm -f $diff_file
		else
			echo "Warning: line info file ($diff_file) does not exist" >&2
		fi
	fi
}

action=remove
if [ "$1" == "-r" ]; then
	action=add
	shift
fi

p=`pwd`
full=`echo $1 | sed -e "s#\(^[^\/]\)#$p/\1#"`
if [ -f "$full" ]; then
	echo $full | grep .smali$ > /dev/null && rm_line $action $full
	exit 0
fi

echo "$full"

for file in `find $full -name "*.smali"`
do
    rm_line $action $file $2
done
